package com.dmz.workflow.util;

import com.echronos.system.resp.member.MemberDetailsResp;
import com.dmz.workflow.constant.MsgContentEnum;
import com.dmz.workflow.service.WorkflowProcessRelationsService;
import com.dmz.workflow.wrapper.MemberFeignWrapper;
import io.jsonwebtoken.lang.Collections;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CommonMessageManager {
    @Autowired
    private MessageContentWrapper messageContentWrapper;
    @Autowired
    private MemberFeignWrapper memberFeignWrapper;
    @Autowired
    private ImcComponent imcComponent;
    @Autowired
    private TaskService taskService;
    @Autowired
    private WorkflowProcessRelationsService workflowProcessRelationsService;

    /**
     * 查询流程当前的审批人并发送消息
     * @param processInstanceId 流程实例ID
     */
    public void sendAssistantMessageByProcessInstanceId(String processInstanceId) {
        List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstanceId).list();
        List<Integer> memberIds = new ArrayList<>();
        for (Task task : tasks) {
            if (ObjectUtils.isNotEmpty(task.getAssignee())) {
                memberIds.add(Integer.valueOf(task.getAssignee()));
            }
        }
        if (!Collections.isEmpty(memberIds)) {
            List<MemberDetailsResp> memberDetailsResps = memberFeignWrapper.queryMemberDetailList(memberIds);
            List<Integer> userIds = new ArrayList<>();
            for (MemberDetailsResp memberDetailsResp : memberDetailsResps) {
                userIds.add(memberDetailsResp.getUserId());
            }
            imcComponent.imcAssistantSender(messageContentWrapper.generateNotCommentMessageBody(MsgContentEnum.MESSAGE_AUDIT_CONTENT, processInstanceId),
                    null, userIds, null);
        }
    }
}
